//
//  WalletViewController.m
//  Molian
//
//  Created by wansong on 4/9/15.
//  Copyright (c) 2015 xiaocan. All rights reserved.
//

#import "WalletViewController.h"

#import "RechargeViewController.h"
#import "WithdrawViewController.h"
#import "PayViewController.h"
#import "WalletRecordViewController.h"

@interface WalletViewController ()
{
    UILabel *moneyLabel;
}
@end

@implementation WalletViewController

#pragma mark life cycle
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    moneyLabel.text = [NSString stringWithFormat:@"%.0f 时光", ([MLUser currentUser].balance*10)];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的钱包";
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    // 从导航条以下开始计算
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    MLUser *user = [MLUser currentUser];
    [user refreshInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        moneyLabel.text = [NSString stringWithFormat:@"%.0f  时光",(user.balance*10)];
        
        // 提现按钮
        UIButton *payBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 120, DeviceWidth-30, 45)];
        payBtn.layer.masksToBounds = YES;
       
        [payBtn.layer setCornerRadius:22.5f];
        [payBtn setBackgroundImage:[UIImage imageWithColor:RGBA(87, 221, 121, 1)] forState:UIControlStateNormal];
        [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [payBtn setTitle:@"充值" forState:UIControlStateNormal];
        payBtn.titleLabel.font = [UIFont fontWithName:FontName size:16];
        [payBtn addTarget:self action:@selector(goToRecharge) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:payBtn];
        
        if (user.type != MLUserType_NORMAL&&[Utils cheackConfigurationWithdraw]) {
            // 提现按钮
            UIButton *withdrawBtn = [[UIButton alloc]initWithFrame:CGRectMake(15, 120+60, DeviceWidth-30, 45)];
            withdrawBtn.layer.masksToBounds = YES;
            [withdrawBtn.layer setCornerRadius:22.5f];
//            payBtn.layer.borderWidth = 0.5;
//            payBtn.layer.borderColor = RGBA(87, 221, 121, 1).CGColor;
            [withdrawBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [withdrawBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [withdrawBtn setTitle:@"提现" forState:UIControlStateNormal];
            [withdrawBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            withdrawBtn.titleLabel.font = [UIFont fontWithName:FontName size:16];
            [withdrawBtn addTarget:self action:@selector(goToWithdraw) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:withdrawBtn];
        }
        
        
    }];
    
    // 金额显示
    UIView *moneyView = [[UIView alloc]initWithFrame:CGRectMake(0, 30, DeviceWidth, 60)];
    moneyView.clipsToBounds = YES;
    moneyView.backgroundColor = [UIColor whiteColor];
    [moneyView.layer setBorderWidth:0.5];
    [moneyView.layer setBorderColor:RGBA(229, 229, 229, 1).CGColor];
    [self.view addSubview:moneyView];
    
    UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 30)];
    titleLable.font = [UIFont fontWithName:FontName size:16];
    titleLable.textAlignment = NSTextAlignmentLeft;
    titleLable.textColor = [UIColor blackColor];
    titleLable.text = @"钱包余额";
    [moneyView addSubview:titleLable];
    
    // 余额值
    moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(DeviceWidth-(DeviceWidth/2)-20, 15, DeviceWidth/2, 30)];
    moneyLabel.font = [UIFont fontWithName:FontName size:14];
    moneyLabel.textAlignment = NSTextAlignmentRight;
    moneyLabel.textColor = [UIColor grayColor];
    moneyLabel.text = [NSString stringWithFormat:@"%.0f 时光",(user.balance*10)];
    [moneyView addSubview:moneyLabel];
    
    UIButton *ringetBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [ringetBt sizeToFit];
    [ringetBt setTitle:@"收支记录" forState:UIControlStateNormal];
    ringetBt.titleLabel.font = kFont(15);
    ringetBt.frame = CGRectMake(0, 0, 60, 30);
    ringetBt.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [ringetBt addTarget:self action:@selector(clickRingetBt) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *ringetBatBt = [[UIBarButtonItem alloc] initWithCustomView:ringetBt];
    self.navigationItem.rightBarButtonItem = ringetBatBt;
    
}
- (void)clickRingetBt
{
    
    WalletRecordViewController *walletRVC = [[WalletRecordViewController alloc] init];
    walletRVC.title = @"收支记录";
    [self.navigationController pushViewController:walletRVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}
#pragma mark user interaction

// 去充值
- (void)goToRecharge {
    [AVAnalytics event:@"我的钱包充值"]; // 通知服务器一个点击事件。
    //支付宝充值
//    AlipayViewController *alipayVC = [[AlipayViewController alloc] init];
//    [self.navigationController pushViewController:alipayVC animated:YES];
//    //苹果内购
    [Utils pushTabbarHidden:[[PayViewController alloc]init] Selfvc:self];
//    RechargeViewController *rechargeView = [[RechargeViewController alloc]init];
//    [self.navigationController pushViewController:rechargeView animated:YES];
}

// 去提现
- (void)goToWithdraw {
    [AVAnalytics event:@"提现"]; // 通知服务器一个点击事件。
    WithdrawViewController *withdrawView = [[WithdrawViewController alloc]init];
    [self.navigationController pushViewController:withdrawView animated:YES];
}

@end
